<template>
	<view class="padding">
		<view class="text-white padding bg radius">
			<u-form :model="form" ref="uForm" label-position="top" :label-style='lableStyle'>
				<u-form-item label="发布标题">
					<u-input v-model="form.myLevel" placeholder="请填写（必填）" />
				</u-form-item>
				<!-- <u-form-item label="用户分类">
					<u-radio-group v-model="form.authentication" @change="radioGroupChange" :width="radioCheckWidth"
						:wrap="radioCheckWrap">
						<u-radio shape="circle" active-color="#557EFD" v-for="(item, index) in radioList" :key="index"
							:name="item.val" :disabled="item.disabled">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</u-form-item> -->
				<u-form-item label="师傅地址">
					<u-input v-model="form.region" disabled="true" placeholder="请填写 (必填)" @click="goSelectCity()" />
				</u-form-item>
				<u-form-item label="家政分类">
					<!-- <u-input v-model="form.game" disabled placeholder="可商议" @click="show = true" /> -->
					<view v-if="game == ''" @click="show = true">
						选择家政分类（多选）
					</view>
					<view v-else style="display: inline-flex;" @click="show = true">
						<view v-for="(item,index) in game"
							style="background: #557EFD;margin-right: 15rpx;color: #FFFFFF;padding: 0rpx 15rpx;border-radius: 15rpx;">
							{{item}}
						</view>
					</view>
				</u-form-item>
				<u-form-item label="接单价格">
					<u-input v-model="form.money" type="number" placeholder="请填写（必填）" />
				</u-form-item>
				<u-form-item label="最低时间限制">
					<u-input v-model="form.minNum" disabled="true" type="number" placeholder="请填写（必填）"
						@click="showData = true" />
				</u-form-item>
				<u-form-item label="接单单位">
					<u-radio-group v-model="form.unit" @change="jiedanChange">
						<u-radio shape="circle" active-color="#557EFD" v-for="(item, index) in jiedanList" :key="index"
							:name="item.name">
							{{ item.name }}
						</u-radio>
					</u-radio-group>
				</u-form-item>
			</u-form>
		</view>

		<view class="text-white padding bg radius margin-tb">
			<view class="text-lg">上传主页图</view>
			<view class="margin-top"
				style="border: 2rpx dashed #484B74; width: 100%;height: 320rpx;position: relative;">
				<view style="text-align: center;margin: 80rpx auto 0;" @click="addImage(1)" v-if="!form.homepageImg">
					<image src="../static/photo.png" mode="widthFix" style="width: 73rpx;"></image>
					<view class="text-sm text-gray margin-top-sm">添加图片(可添加技能图/个人照片)</view>
				</view>
				<image @click="addImage(1)" v-else :src="form.homepageImg" style="width: 100%;height: 320rpx;"></image>
			</view>
		</view>

		<view class="text-white padding bg radius margin-tb">
			<view class="text-lg">上传详情图</view>

			<view class="flex" style="overflow: hidden;flex-direction: initial;flex-wrap: wrap;">
				<view v-if="Imgo.length">
					<view class="margin-top flex margin-right-sm flex-wrap">
						<view class="flex" style="width: 200rpx;height: 200rpx;margin-right: 5rpx;position: relative;"
							v-for="(image,index) in Imgo" :key="index">
							<image :src="image" style="width: 100%;height: 100%;"></image>
							<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
								@click="removeImg(index)">
								<u-icon name="close-circle-fill" color="#2979ff" size="50rpx"></u-icon>
							</view>

						</view>
					</view>
				</view>
				<view class="margin-top" @click="addImages(2)" v-if="Imgo.length<=5">
					<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;"
						class="flex justify-center align-center">
						<view>
							<view class="text-center">
								<u-icon name="plus" color="#666666" size="28"></u-icon>
							</view>
							<view class="text-center">选择图片</view>
						</view>
					</view>
				</view>

			</view>
		</view>

		<!-- <u-select v-model="show" mode="single-column" :list="gameList" @confirm="selConfirm"></u-select> -->
		<u-popup v-model="show" mode="bottom" border-radius="14" :closeable="true">
			<view class="fl_pop">
				<view style="padding: 100rpx 60rpx;">
					<u-checkbox-group @change="checkboxGroupChange" size="38" max="4">
						<u-checkbox @change="checkboxChange" v-model="item.checked" v-for="(item, index) in gameList"
							:key="index" :name="item.gameName" active-color="#557EFD">{{item.gameName}}</u-checkbox>
					</u-checkbox-group>
					<u-button @click="bindGame()" :custom-style="customStyle1">确定</u-button>
				</view>
			</view>
		</u-popup>

		<u-picker v-model="isShowTime" mode="time" zIndex='999999' :params="params" @confirm="statusChange"></u-picker>
		<u-picker v-model="isEndTime" mode="time" zIndex='999999' :params="params" @confirm="endChange"></u-picker>

		<u-button v-if="id" @click="update" class="margin-top" :custom-style="customStyle" shape="circle"
			:hair-line="false">发布</u-button>
		<u-button v-else @click="submit" class="margin-top" :custom-style="customStyle" shape="circle"
			:hair-line="false">发布</u-button>

		<!-- 地区选择 -->
		<!-- <u-picker v-model="Dqshow" mode="region" @confirm="bindRegion"></u-picker> -->

		<u-action-sheet :list="actionSheetList" v-model="actionSheetShow" @click="actionSheetCallback"></u-action-sheet>
		<u-select v-model="showData" mode="mutil-column" :list="listDta" @confirm="confirm"></u-select>
	</view>
</template>

<script>
	import configdata from '@/common/config.js';
	import jsfunRecord from '../components/jc-record/jc-record.vue'

	export default {
		components: {
			jsfunRecord
		},
		data() {
			return {
				time: '',
				startTime: '开始时间（必填）',
				startHour: '',
				endTime: '结束时间（必填）',
				endHour: '',

				isShowTime: false,
				isEndTime: false,
				params: {
					year: false,
					month: false,
					day: false,
					hour: true,
					minute: true,
				},
				form: {
					authentication: 1,
					game: '',
					gameId: '',
					orderTakingTime: '',
					orderTakingArea: '',
					money: '',
					voiceIntroduce: '',
					sec: 0,
					homepageImg: '',
					detailsImg: [],
					longitude: '',
					latitude: '',
					city: '',
					id: 0,
					unit: '小时',
					myLevel: '',
					minNum: '',
					region: '',
					detailadd: ''
				},
				radioList: [{
					name: '用户已认证',
					checked: true,
					disabled: false,
					val: 1
				}, {
					name: '企业已认证',
					checked: true,
					disabled: false,
					val: 2
				}],
				disabled: false,
				jiedanList: [{
					name: '小时',
					checked: true,
					disabled: 1
				}, {
					name: '天',
					checked: false,
					disabled: 2
				}],
				lableStyle: {
					// color: '#FFFFFF',
					fontSize: '32rpx'
				},
				customStyle: {
					backgroundColor: '#557EFD',
					color: '#FFFFFF',
					border: 0
				},
				status: 1,
				customStyle1: {
					color: '#FFFFFF',
					position: "fixed",
					bottom: "15rpx",
					left: "0rpx",
					right: "0rpx",
					margin: "0rpx 50rpx"
				},
				showData: false,
				listDta: [
					[{
							value: '1',
							label: '1'
						},
						{
							value: '2',
							label: '2'
						},
						{
							value: '3',
							label: '3'
						},
						{
							value: '4',
							label: '4'
						},
						{
							value: '5',
							label: '5'
						},
						{
							value: '6',
							label: '6'
						},
						{
							value: '7',
							label: '7'
						},
						{
							value: '8',
							label: '8'
						},
						{
							value: '9',
							label: '9'
						},
						{
							value: '10',
							label: '10'
						}
					]
				],
				//录音相关参数
				// #ifndef H5
				//H5不能录音
				RECORDER: uni.getRecorderManager(),
				// #endif

				recording: false,
				willStop: false,

				recordLength: 0, //录音时长

				show: false,
				gameList: [],
				listgame: '',
				game: '',
				checked: false,
				disabled: false,

				number: 0,
				homepageImg: '',
				id: '',
				isPlay: false,

				Imgo: [],

				isAuthentication: 0,
				Dqshow: false,
				// region: ''
			}
		},
		onLoad(option) {
			// console.log(option.city)

			this.getGameList()
			this.id = option.id
			this.form.id = option.id

			this.form.city = uni.getStorageSync('city')
			// this.form.latitude = uni.getStorageSync('latitude')
			// this.form.longitude = uni.getStorageSync('longitude')

			this.isAuthentication = uni.getStorageSync('isAuthentication')
			if (this.isAuthentication == 1) {
				this.radioList[1].disabled = true
				console.log(this.radioList[1])
			} else if (this.isAuthentication == 2) {
				this.radioList[0].disabled = true
				console.log(this.radioList[0])
			}
			this.getData(this.id)
		},
		onShow() {


		},
		methods: {
			//获取省市区
			city(latitude, longitude) {
				this.$Request.get("/app/Login/selectCity", {
					lat: latitude,
					lng: longitude
				}).then(res => {
					console.log(res)
					this.form.region = res.data.city
					// this.region = res.data.city
					// this.address = res.data.province + res.data.city + res.data.district
					console.log(this.form, 'form')
				});
			},
			//地区选择
			goSelectCity() {
				// uni.navigateTo({
				// 	url: '/pages/index/citys/citys?id='+1
				// });
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.form.latitude = res.latitude
						that.form.longitude = res.longitude
						that.form.detailadd = res.address + res.name
						that.city(res.latitude, res.longitude)
					}
				});
			},
			// bindOpen() {
			// 	this.Dqshow = true
			// },
			// bindRegion(e) {
			// 	console.log(e)
			// 	this.region = e.province.label + e.city.label + e.area.label
			// 	this.form.region = e.province.label + ',' + e.city.label + ',' + e.area.label
			// },
			confirm(e) {

				this.form.minNum = e[0].value

				console.log(e)
			},
			// 详情图删除
			removeImg(index) {
				console.log(index)
				this.Imgo.splice(index, 1)
			},
			// 选中某个复选框时，由checkbox时触发
			checkboxChange(e) {
				// console.log(e);
				this.checked = e
				this.listgame = e
			},
			// 选中任一checkbox时，由checkbox-group触发
			checkboxGroupChange(e) {

				this.listgame = e
				console.log(this.listgame)
			},
			// 复选框选中提交
			bindGame() {
				console.log(this.listgame, '11111111')

				this.game = this.listgame
				console.log(this.form.game, '5555555')
				let gameId = "";
				for (var game in this.listgame) {
					gameId = gameId + "," + this.listgame[game]
				}
				gameId = gameId.substring(1, gameId.length);
				this.form.gameId = gameId

				this.show = false
			},
			// radio选择发生变化
			radioGroupChange(e) {
				this.form.classify = e;
			},
			jiedanChange(e) {
				this.form.unit = e;
			},
			saveRecord(recordPath) {
				console.log("===音频文件地址：" + recordPath + "===")
				uni.showLoading({
					title: '录音上传中...'
				})
				console.log(this.config("APIHOST1") + '/alioss/uploadMusic')
				uni.uploadFile({
					// url: this.config("APIHOST1") + '/alioss/uploadMusic', //仅为示例，非真实的接口地址
					url:this.config("https://jiazhengsfv5.xianmaxiong.com") + '/alioss/uploadMusic',
					filePath: recordPath,
					name: 'file',
					success: (uploadFileRes) => {
						console.log(JSON.parse(uploadFileRes.data))
						this.form.voiceIntroduce = JSON.parse(uploadFileRes.data).data.url
						this.form.sec = JSON.parse(uploadFileRes.data).data.sec
						this.recordLength = JSON.parse(uploadFileRes.data).data.sec
						uni.hideLoading()
					},
					fail: (err) => {
						console.log(err)
						uni.hideLoading()
					}
				});
			},
			select(e) {
				this.status = e
			},
			statusChange(e) {
				this.startHour = e.hour
				this.startTime = e.hour + ':' + e.minute
			},
			endChange(e) {
				if (this.startHour >= e.hour) {
					uni.showToast({
						title: '结束时间必须大于开始时间一个小时以上',
						icon: "none"
					})
					return
				}
				this.endTime = e.hour + ':' + e.minute
				this.form.orderTakingTime = this.startTime + '~' + this.endTime
			},
			// 图片上传
			addImages(e) {
				let that = this
				uni.chooseImage({
					count: 6,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 2) {
										// that.Imgo = JSON.parse(uploadFileRes.data).data
										console.log(JSON.parse(uploadFileRes.data).data)
										that.Imgo.push(JSON.parse(uploadFileRes.data).data)
										// that.form.detailsImg.push(that.Imgo)
									}
									console.log(that.form.detailsImg)
									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			// 图片上传
			addImage(e) {
				let that = this
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							that.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://jiazhengsfv5.xianmaxiong.com/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									if (e == 1) {
										that.form.homepageImg = JSON.parse(uploadFileRes.data).data
									}

									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
			// 发布
			submit() {
				this.form.detailsImg = this.Imgo
				this.form.detailsImg = this.form.detailsImg.toString();

				if (!this.form.myLevel) {
					uni.showToast({
						title: '请填写发布标题',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.region) {
					uni.showToast({
						title: '请填写服务地区 ',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (this.game == '') {
					uni.showToast({
						title: '请选择发布分类',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.money) {
					uni.showToast({
						title: '请填写接单价格',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.minNum) {
					uni.showToast({
						title: '请填写时间范围',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.homepageImg) {
					uni.showToast({
						title: '请上传主页图',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.detailsImg) {
					uni.showToast({
						title: '请上传详情图',
						icon: 'none',
						duration: 1000
					})
					return
				}
				console.log(this.form)
				this.form.id = ''
				this.$Request.get("/app/orderTaking/insertOrderTaking", this.form).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				});
			},
			// 重新编辑
			update() {

				this.form.detailsImg = this.Imgo
				this.form.detailsImg = this.form.detailsImg.toString();

				if (!this.form.myLevel) {
					uni.showToast({
						title: '请填写发布标题',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.region) {
					uni.showToast({
						title: '请填写服务地区 ',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (this.game == '') {
					uni.showToast({
						title: '请选择发布分类',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.money) {
					uni.showToast({
						title: '请填写接单价格',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.minNum) {
					uni.showToast({
						title: '请填写时间范围',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.homepageImg) {
					uni.showToast({
						title: '请上传主页图',
						icon: 'none',
						duration: 1000
					})
					return
				}
				if (!this.form.detailsImg) {
					uni.showToast({
						title: '请上传详情图',
						icon: 'none',
						duration: 1000
					})
					return
				}
				this.$Request.get("/app/orderTaking/updateTakingOrder", this.form).then(res => {
					if (res.code == 0) {
						uni.showToast({
							title: '编辑成功',
							icon: 'none'
						})
						setTimeout(function() {
							uni.navigateBack()
						}, 1000)
					}
				});
			},
			// 获取游戏列表
			getGameList() {
				this.$Request.get("/app/appGame/queryGameName").then(res => {
					if (res.code == 0) {
						res.data.forEach(res => {
							res.label = res.gameName
							res.value = res.id
						})
						this.gameList = res.data
					}
				});
			},
			// 选择游戏
			selConfirm(e) {
				// console.log(e);
				this.form.game = e[0].label
				this.form.gameId = e[0].value
			},
			getData(e) {
				let data = {
					id: e
				}
				this.$Request.get("/app/orderTaking/queryTakingOrder", data).then(res => {
					if (res.code == 0) {
						this.form.unit = res.data.unit

						this.game = res.data.gameId.split(',')
						this.form.gameId = res.data.gameId
						this.form.myLevel = res.data.myLevel
						this.form.money = res.data.money
						this.form.unit = res.data.unit
						this.form.homepageImg = res.data.homepageImg ? res.data.homepageImg : ''
						this.Imgo = res.data.detailsImg.split(',') ? res.data.detailsImg.split(',') : ''
						// uni.setStorageSync('isAuthentication', res.data.authentication)
						this.form.authentication = res.data.authentication
						this.form.region = res.data.region
						this.form.region = res.data.region
						this.form.minNum = res.data.minNum

						this.form.latitude = res.data.latitude
						this.form.longitude = res.data.longitude
						this.form.detailadd = res.data.detailadd
				
					}
				});
			},
			// 清除录音
			clearAudio() {
				this.status = 1
				this.recordLength = 0
				this.form.voiceIntroduce = ''
			},


		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}

	.bg {
		background-color: #FFFFFF;
	}

	.u-size-default {
		background: #557EFD !important;
	}
</style>
